package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/12
 */
@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupItemService checkGroupItemService;

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        return this.list(queryWrapper);
    }

    @Override
    public boolean deleteById(Long id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setDeleteFlag(1);
        this.updateById(checkGroup);
        QueryWrapper<CheckGroupItem> queryWrapper = new QueryWrapper<CheckGroupItem>().eq("group_id", id);
        checkGroupItemService.remove(queryWrapper);
        return true;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        if (queryPageBean.getQueryString()!=null&&"".equals(queryPageBean.getQueryString().trim())==false){
            queryWrapper.and(wrapper->{
                wrapper.like("code",queryPageBean.getQueryString())
                        .or().like("name",queryPageBean.getQueryString())
                        .or().like("help_code",queryPageBean.getQueryString());
            });
        }
        this.page(page,queryWrapper);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    @Transactional
    public boolean saveGroupAndItem(CheckGroupDto checkGroupDto) {
        //1.保存检查组信息
        this.saveOrUpdate(checkGroupDto.getCheckGroup());
        //2.根据GroupId删除中间表信息
        checkGroupItemService.removeById(checkGroupDto.getCheckGroup().getId());
        //3.循环往中间表中插入数据
        ArrayList<CheckGroupItem> listItems = new ArrayList<>();
        for (Long itemId : checkGroupDto.getItemIds()) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setItemId(itemId);
            checkGroupItem.setGroupId(checkGroupDto.getCheckGroup().getId());
            listItems.add(checkGroupItem);
        }
            checkGroupItemService.saveBatch(listItems);
        return true;
    }

    @Override
    public List<CheckGroupVo> findCheckGroupBySetMealId(Long setmealId) {
        return this.baseMapper.findCheckGroupBySetMealId(setmealId);
    }


}
